<?php

    // +----------------------------------------------------------------------
    // | 留言信息
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\controller\cms\admin;

    use app\model\cms\LeaveMessage as LeaveMessageModel;
    use yicmf\builder\Edit;
    use yicmf\builder\Table;
    use app\controller\admin\Common;
    use yicmf\builder\View;

    class LeaveMessage extends Common
    {

        protected function initialize()
        {
            parent::initialize();
        }

        public function _filter()
        {
            return [];
        }

        /**
         * 列表.
         */
        public function index()
        {
            $statusOptions = [
                [
                    'id' => 1,
                    'value' => '待回复'
                ],
                [
                    'id' => 2,
                    'value' => '已回复'
                ]
            ];
            // 显示页面
            $builder = new Table();
            $builder->model(LeaveMessageModel::class)
				->where($this->_filter())
				->searchSelect('status', '状态：', $statusOptions)
                ->searchText('name', '称呼：', '留言用户的称呼')
                ->keyId('id', '编号', 'desc')
                ->keyText('name', '称呼')
                ->keyText('sex', '性别')
                ->keyText('mobile', '联系手机')
                ->keyText('content', '留言内容')
                ->keyText('create_time', '留言时间')
                ->keyBool('is_show', '是否显示')
                ->keyStatus(LeaveMessageModel::$status_text)
                ->groupOutxls()
                ->groupOutxlsCheck()
                ->actionView('view?id={$id}')
                ->actionDelete('del?id={$id}')
                ->actionUpdate('update?id={$id}', '回复', [1]);
            return $builder->fetch();
        }

        /**
         * 用户留言信息更新.
         */
        public function update()
        {
            if ($this->request->isPost()) {
                $result = $this->validate($this->request->post(), 'Message.relay');
                if (true === $result) {
                    if ($this->model->relay($this->request->post()) !== false) {
                        $this->_returnSuccess( '回复留言成功', true);
                    } else {
                        $this->_returnError( '回复留言失败，失败信息【' . $this->model->getError() . '】');
                    }
                } else {
                    $this->_returnError( '回复留言失败，数据验证错误【' . $result . '】');
                }
            } else {
                if (!$this->request->has('id', 'get')) {
                    $this->_returnError( '选择异常');
                } else {
                    $data = $this->model->get($this->request->get('id'));
                    $data['status'] = 2;
                    $builder = new Edit();
                    $builder->title('回复留言')
                        ->keyLabel('name', '留言称呼')
                        ->keyLabel('email', '邮箱')
                        ->keyLabel('phone', '电话')
                        ->keyLabel('content', '用户留言内容')
                        ->keyTextArea('reply', '回复内容', '用户留言的回复内容，不支持编辑')
                        ->keyRadio('send_info', '是否发生通知', $this->model->send_info_text, '需要相关插件支持')
                        ->keyBool('is_show', '是否在首页显示')
                        ->data($data)
                        ->buttonSubmit('', '保存')
                        ->buttonClose();
                    return $builder->fetch();
                }
            }
        }

        public function view()
        {
            $data = $this->model->get($this->request->get('id'));
            $view = new View();
            $view->keyLabel('name', '留言称呼')
                ->keyLabel('email', '邮箱')
                ->keyLabel('phone', '电话')
                ->keyLabel('content', '用户留言内容')
                ->keyLabel('reply', '回复内容', '用户留言的回复内容，不支持编辑')
                ->keyLabel('send_info_text', '是否发生通知')
                ->keyLabel('create_time', '留言时间')
                ->keyLabel('status_text', '状态')
                ->data($data);
            return $view->fetch();
        }
    }
